package com.fjzx.androidframe.ui.textinput;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fjzx.androidframe.ui.R;

import org.w3c.dom.Text;

/**
 * @author xiongyongcong
 */
public class ValidateEditText extends LinearLayout implements TextWatcher {
    private static final int GRAVITY_LEFT = 0;
    private static final int GRAVITY_RIGHT = 1;

    private TextView mErrorText;
    private EditText mEditText;
    private int textGravity;
    private int errorGravity;
    private boolean editable;
    private String hint;

    public ValidateEditText(Context context) {
        this(context, null);
    }

    public ValidateEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ValidateEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ValidateEditText);
        textGravity = typedArray.getInt(R.styleable.ValidateEditText_textGravity, -1);
        errorGravity = typedArray.getInt(R.styleable.ValidateEditText_errorGravity, -1);
        hint = typedArray.getString(R.styleable.ValidateEditText_hint);
        editable = typedArray.getBoolean(R.styleable.ValidateEditText_editable, true);
        typedArray.recycle();

        setOrientation(VERTICAL);
        addEditTextView(context, attrs, defStyleAttr);
        addErrorTextView(context);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        super.dispatchTouchEvent(ev);
        return editable;
    }

    private void addEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        mEditText = (EditText) LayoutInflater.from(context).inflate(R.layout.base_form_edittext, this, false);
        mEditText.addTextChangedListener(this);
        mEditText.setEnabled(editable);
        mEditText.setHint(hint);
        if (textGravity != -1) {
            mEditText.setGravity(textGravity);
        }
        addView(mEditText);
    }

    private void addErrorTextView(Context context) {
        mErrorText = (TextView) LayoutInflater.from(context).inflate(R.layout.base_form_errortext, this, false);
        if (errorGravity != -1) {
            mErrorText.setGravity(errorGravity);
        }
        addView(mErrorText);
    }

    public EditText getEditTextView() {
        return mEditText;
    }

    public TextView getErrorTextView() {
        return mErrorText;
    }

    public Editable getText() {
        return mEditText.getText();
    }

    public void setError(CharSequence text) {
        mErrorText.setVisibility(TextUtils.isEmpty(text) ? GONE : VISIBLE);
        mErrorText.setText(text);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(s)) {
            setError(null);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
}
